package com.yunhai.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.fms.FmsCodeDictEntity;
import com.yunhai.model.entity.fms.FmsRecipeCategoryEntity;
import com.yunhai.model.vo.fms.FmsRecipeCategoryQuery;
import com.yunhai.model.vo.fms.FmsRecipeCategoryVo;
import com.yunhai.recipe.mapper.FmsRecipeCategoryMapper;
import com.yunhai.recipe.service.FmsCodeDictService;
import com.yunhai.recipe.service.FmsRecipeCategoryService;
import org.apache.commons.beanutils.PropertyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class FmsRecipeCategoryServiceImpl implements FmsRecipeCategoryService {

    @Resource
    private FmsRecipeCategoryMapper fmsRecipeCategoryMapper;
    @Resource
    private FmsCodeDictService fmsCodeDictService;

    /**
     * 获取 FmsRecipeCategory 分页列表
     */
    @Transactional
    public IPage getFmsRecipeCategoryPagination(PaginationVo paginationVo, FmsRecipeCategoryQuery fmsRecipeCategoryQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<FmsRecipeCategoryEntity> lqs = new LambdaQueryWrapper();
        lqs.orderByDesc(FmsRecipeCategoryEntity::getCreateTime);
        lqs.eq(fmsRecipeCategoryQuery.getEnabled()!=null, FmsRecipeCategoryEntity::getEnabled, fmsRecipeCategoryQuery.getEnabled());
        lqs.likeRight(fmsRecipeCategoryQuery.getNumber() !=null, FmsRecipeCategoryEntity::getNumber, fmsRecipeCategoryQuery.getNumber());
        lqs.likeRight(fmsRecipeCategoryQuery.getName() !=null, FmsRecipeCategoryEntity::getName, fmsRecipeCategoryQuery.getName());

        fmsRecipeCategoryMapper.selectPage(page, lqs);


        List<FmsRecipeCategoryEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 FmsRecipeCategory 所有数据
     *
    */
    @Transactional
    public List<FmsRecipeCategoryEntity> getFmsRecipeCategoryAllList( FmsRecipeCategoryQuery fmsRecipeCategoryQuery ) throws Exception {

        LambdaQueryWrapper<FmsRecipeCategoryEntity> lqs = new LambdaQueryWrapper();
        List<FmsRecipeCategoryEntity> list = fmsRecipeCategoryMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 FmsRecipeCategory 详情
    */
    @Transactional
    public FmsRecipeCategoryEntity getFmsRecipeCategoryDetail( Long id) throws Exception {

        FmsRecipeCategoryEntity fmsRecipeCategory = fmsRecipeCategoryMapper.selectById(id);
        return fmsRecipeCategory;
    }


    /**
     * 新增 FmsRecipeCategory
     * */
    @Transactional
    public void addFmsRecipeCategory(FmsRecipeCategoryVo fmsRecipeCategoryVo) throws Exception {

        FmsRecipeCategoryEntity fmsRecipeCategory = new FmsRecipeCategoryEntity();
        ReflectionCopy.copyProperties( fmsRecipeCategory, fmsRecipeCategoryVo );
        fmsRecipeCategoryMapper.insert(fmsRecipeCategory);

        fmsCodeDictService.updateById( 1L, fmsRecipeCategoryVo.getNumber());
        return;
    }


    /**
     * 更新 FmsRecipeCategory
     * */
    @Transactional
    public void updateFmsRecipeCategory( FmsRecipeCategoryVo fmsRecipeCategoryVo) throws Exception {

        if( fmsRecipeCategoryVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        FmsRecipeCategoryEntity fmsRecipeCategory = new FmsRecipeCategoryEntity();
        ReflectionCopy.copyProperties( fmsRecipeCategory, fmsRecipeCategoryVo );
        fmsRecipeCategoryMapper.updateById(fmsRecipeCategory);
        return;
    }

    /**
     * 删除 FmsRecipeCategory
     * */
    @Transactional
    public void batchRemoveFmsRecipeCategory( List<Long> ids ) throws Exception {


        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        fmsRecipeCategoryMapper.deleteBatchIds(ids);
    }

}

      